package top.z.orm.handle.file;

import top.z.orm.handle.osql.core.Logger;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class FileHandle {
    // 换行符
    private final static String LINE_SEPARATOR = System.getProperty("line.separator");

    public static void createFile(String filePath, String content) {
        Path f = Paths.get(filePath);
        try (BufferedWriter writer = Files.newBufferedWriter(f)) {
            writer.write(content);
        } catch (Exception e) {
            e.printStackTrace();
            Logger.error(e.getMessage());
        }
    }

    public static void appendFile(String filePath, String content) {
        Path f = Paths.get(filePath);
        try (BufferedWriter writer = Files.newBufferedWriter(f, StandardOpenOption.APPEND)) {
            writer.write(LINE_SEPARATOR + content);
        } catch (Exception e) {
            e.printStackTrace();
            Logger.error(e.getMessage());
        }
    }

    public static BufferedReader readFile(String fullPathAndName) {
        File           file   = new File(fullPathAndName);
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
        } catch (IOException e) {
            Logger.error(e.getMessage());
        }
        return reader;
    }
}